package com.longma.server.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.longma.server.entity.InspectionRecord;
import com.longma.server.pojo.UserResponseEntity;
import com.longma.server.pojo.dto.InspectionRecordAddDTO;
import com.longma.server.pojo.dto.InspectionRecordUpdateDTO;
import com.longma.server.pojo.dto.PageRequestDTO;
import com.longma.server.service.IInspectionRecordService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author HAIBO
 * @since 2021-04-28
 */
@RestController
@RequestMapping("/api/inspection-record")
public class InspectionRecordController {
    @Autowired
    IInspectionRecordService iInspectionRecordService;

    @ApiOperation(value = "新增巡視记录")
    @PostMapping("/add")
    public UserResponseEntity<InspectionRecord> addInspectionRecord(@Valid @RequestBody InspectionRecordAddDTO addRequestDTO){
        return UserResponseEntity.success(iInspectionRecordService.addInspectionRecord(addRequestDTO));
    }

    @ApiOperation(value = "根据巡視记录id删除巡視记录")
    @PostMapping("/del")
    public UserResponseEntity<Boolean> deleteInspectionRecordById(@NotNull(message = "巡視记录id不能为空")  @RequestParam Integer id){
        return UserResponseEntity.success(iInspectionRecordService.deleteInspectionRecordById(id));
    }

    @ApiOperation(value = "根据巡視记录id列表删除巡視记录")
    @PostMapping("/dellist")
    public UserResponseEntity<Boolean> deleteInspectionRecordByIds(@RequestParam(name="ids") @Size(min = 1,message = "巡視记录id列表不能为空")
                                                                       List<@Valid @NotNull(message = "巡視记录id不能为空") Integer> ids){
        return UserResponseEntity.success(iInspectionRecordService.deleteInspectionRecordByIds(ids));
    }

    @ApiOperation(value = "更新巡視记录")
    @PostMapping("/update")
    public UserResponseEntity<InspectionRecord> updateInspectionRecordSetting(@Valid @RequestBody InspectionRecordUpdateDTO updateRequestDTO){
        return UserResponseEntity.success(iInspectionRecordService.updateInspectionRecord(updateRequestDTO));
    }

    @ApiOperation(value = "获取所有巡視记录(分页)")
    @PostMapping("/list")
    public UserResponseEntity<IPage<InspectionRecord>> getInspectionRecordByPage(@ApiParam(value = "分页参数",required = true) @Valid @RequestBody PageRequestDTO pageRequestDTO){
        return UserResponseEntity.success(iInspectionRecordService.getInspectionRecordByPage(pageRequestDTO.getPageNum(),
                pageRequestDTO.getPageSize(),pageRequestDTO.getAll(),pageRequestDTO.getConditions()));
    }
}

